package com.yh.controller.api;

import com.yh.anno.LoginAuthentication;
import com.yh.config.OtherSource;
import com.yh.service.UserService;
import com.yh.utils.Constants;
import com.yh.utils.ImgUrl;
import com.yh.utils.UploadAndDownload;
import com.yh.vo.LoginVO;
import com.yh.vo.ResponseVO;
import com.yh.vo.UserVO;
import io.minio.MinioClient;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author yuhuan
 * @date 2022/01/13
 */
@RestController
@RequestMapping("/api/user")
public class UserWeb {
    /**
     * 日志
     */
    private static final Logger logger = Logger.getLogger(UserWeb.class);

    @DubboReference(check = false)
    private UserService userService;
    @Autowired
    private OtherSource otherSource;
    @Autowired
    private MinioClient minioClient;

    /**
     * 根据ID查询用户信息（先到缓存找，没结果再到数据库找）
     * 服务地址 http://localhost:8080/api/user/find/id
     */
    @RequestMapping("/find/id")
    @LoginAuthentication
    public ResponseVO findUserById(String userId) {
        UserVO userVO = userService.findUserById(userId);
        return !ObjectUtils.isEmpty(userVO) ? ResponseVO.buildSuccessInstance(userVO) :
                ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT);
    }

    /**
     * 根据ID查询用户的登录信息（仅到缓存找）
     * 服务地址 http://localhost:8080/api/user/login/data
     */
    @RequestMapping("/login/data")
    public ResponseVO findLoginUserById(String userId) {
        UserVO userVO = userService.findLoginUserById(userId);
        return !ObjectUtils.isEmpty(userVO) ? ResponseVO.buildSuccessInstance(userVO) :
                ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT);
    }

    /**
     * 用户注册
     * 服务地址 http://localhost:8080/api/user/register
     */
    @RequestMapping("register")
    public ResponseVO userRegister(@RequestBody LoginVO loginVO) {
        int registerResult = userService.userRegister(loginVO);
        return ResponseVO.buildSuccessInstance(registerResult);
    }

    /**
     * 更新用户数据
     * 服务地址 http://localhost:8080/api/user/update
     */
    @RequestMapping("update")
    @LoginAuthentication
    public ResponseVO updateUserData(UserVO user) {
        MultipartFile headFile = user.getHeadFile();
        // 如果选中了图片就就执行图片的上传和头像信息的修改
        if (!ObjectUtils.isEmpty(headFile) && headFile.getSize() > 0) {
            // 将头像上传到MinIO服务器
            UploadAndDownload.uploadToMinIo(minioClient, otherSource.userFileUrl, headFile);
            // 将头像上传到本次磁盘（Win环境开发测试）
            UploadAndDownload.uploadToDisk(otherSource.uploadPath, headFile);
            // 将头像上传到Nginx本地（Linux环境下的备用路径。MinIO服务器无法使用时的备用选择）
            UploadAndDownload.uploadToNginxLocalhost(otherSource.nginxPath, headFile, otherSource.userFileUrl);
            // 补全用户头像的基本地址
            user.setUserHeadImg(ImgUrl.spliceBasicUrl(otherSource.userFileUrl, headFile.getOriginalFilename()));
        }
        // 将用户信息保存到数据库
        int result = userService.updateUserData(user);
        return ResponseVO.buildSuccessInstance(result);
    }

    /**
     * 修改密码
     * 服务地址 http://localhost:8080/api/user/update/password
     */
    @RequestMapping("update/password")
    @LoginAuthentication
    public ResponseVO updatePassWord(@RequestBody UserVO userVO) {
        int respState = userService.updatePassWord(userVO);
        return ResponseVO.buildSuccessInstance(respState);
    }
}
